/*
 *  Copyright 2002 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *
 */
/* "@(#) RF5_IEK 2.00.01 11-26-02 (swat-c18)" */
/*
 *  ======== thrControl.c ========
 *  This file shows an example of sending messages from one thread to the
 *  other using mailbox.
 */
#include <std.h>

// DSP/BIOS includes
#include <mbx.h>
#include <tsk.h>

// RF includes
#include <utl.h>

// application includes
#include "appResources.h"   // application-wide common info
#include "appThreads.h"     // thread-wide common info
#include "thrControl.h"
#include "appmain.h"

#include "tskProcess.h"     /* contains thread specific information */

// global variables to be modified by GEL
#define NUMCONTROLS 1    // same as the number of processing channels

typedef struct ExternalControl
{
    Int frameRatio[NUMCONTROLS];
    Int quality[NUMCONTROLS];
} ExternalControl;

#pragma DATA_ALIGN(externalControl, 128);
ExternalControl externalControl;        // GEL script writes here
ExternalControl externalControlPrev;    // this is a local copy

/*
 *  ======== thrControlInit ========
 *
 */
Void thrControlInit()
{
    UTL_assert( NUMCONTROLS == 1 );

    // set the default frame ratio
    externalControl.frameRatio[0] = 1;   // live video channel
    externalControl.quality[0]    = 75;
    externalControlPrev = externalControl;
}

void thrControlSet( int FrameRatio, int Quality )
{
    UTL_assert( NUMCONTROLS == 1 );

    // set the default frame ratio
    externalControl.frameRatio[0] = FrameRatio;
    externalControl.quality[0]    = Quality;
}


/*
 *  ======== thrControlStartup ========
 *
 */
Void thrControlStartup()
{
    Int chanNum;
    CtrlMsg txMsg;

    // send message to initialize the frame ratio
    for( chanNum = 0; chanNum < NUMCONTROLS; chanNum++)
    {
        // Set-up initial values for frame ratio
        txMsg.cmd = MSGFRAMECHANGE;
        txMsg.arg1 = chanNum;
        txMsg.arg2 = externalControl.frameRatio[chanNum];

        // Send the request
        MBX_post( &mbxProcess, &txMsg, 0 );

        // Set-up initial values for new quality
        txMsg.cmd  = MSGQUALCHANGE;
        txMsg.arg1 = chanNum;
        txMsg.arg2 = 0;     // JPEG encoder cell index
        txMsg.arg3 = externalControl.quality[chanNum];

        // Send the request
        MBX_post( &mbxProcess, &txMsg, 0 );
    }
}

/*
 *  ======== thrControlRun ========
 *
 *  Main function of Control task.
 */
Void thrControlRun()
{
    Int chanNum;
    CtrlMsg txMsg;

    // Main loop
    while (TRUE)
    {

        // check for changes
        for( chanNum = 0; chanNum < NUMCONTROLS; chanNum++)
        {

            // See if user requested a frame ratio change.
            if( externalControl.frameRatio[chanNum] !=
                externalControlPrev.frameRatio[chanNum] )
            {

                // new value entered
                externalControlPrev.frameRatio[chanNum] =
                    externalControl.frameRatio[chanNum];

                // send message to tell which channel has ratio changed
                txMsg.cmd = MSGFRAMECHANGE;
                txMsg.arg1 = chanNum;
                txMsg.arg2 = externalControl.frameRatio[chanNum];
                MBX_post( &mbxProcess, &txMsg, 0 );
            }

             // See if user requested a quality change.
            if( externalControl.quality[chanNum] !=
                externalControlPrev.quality[chanNum] )
            {
                 // new value entered
                externalControlPrev.quality[chanNum] =
                    externalControl.quality[chanNum];

                // send message to tell which channel has ratio changed
                txMsg.cmd = MSGQUALCHANGE;
                txMsg.arg1 = chanNum;
                txMsg.arg2 = 0;       // JPEG encoder cell index
                txMsg.arg3 = externalControl.quality[chanNum];
                MBX_post( &mbxProcess, &txMsg, 0 );
            }
        }

        // suspend self for 100 ticks, and then poll again
        TSK_sleep( 100 );
    }
}





